package com.appkubes.mall.admin.application.order.rent;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.admin.application.order.order.entity.vo.OrderStatusUpdateVO;
import com.appkubes.mall.admin.application.user.account.account.service.UserAccountService;
import com.appkubes.mall.core.entity.MallOrder;
import com.appkubes.mall.core.service.BaseMallOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author: Rongjie
 * @Date: 2020/2/22
 * @Description:
 */
@Slf4j
@Api(tags = "租赁订单模块")
@RestController
@RequestMapping("/order/rent")
public class OrderRentController {
    @Resource
    BaseMallOrderService baseMallOrderService;

    @Resource
    UserAccountService userAccountService;

    @ApiOperation("更新订单状态")
    @PostMapping("/status/update")
    public Response updateStatus(@ParamValid OrderStatusUpdateVO orderStatusUpdateVO) {
        log.info("updateStatus,UserKey:{},  orderKey: {}", orderStatusUpdateVO.getUserKey(), orderStatusUpdateVO.getOrderKey());
        MallOrder order = baseMallOrderService.getById(orderStatusUpdateVO.getOrderKey());
        if(Objects.equals(orderStatusUpdateVO.getRentStatus(),"2")){  // 订单拒绝
            order.setStatus("cancel");
            order.setFlexSegment2("2");
            baseMallOrderService.updateById(order);
            return Response.success("订单取消");
        }

        order.setFlexSegment2(orderStatusUpdateVO.getRentStatus());
        Boolean flag = baseMallOrderService.updateById(order);
        //确认订单发放佣金
        userAccountService.orderConfirm(orderStatusUpdateVO.getUserKey(), orderStatusUpdateVO.getOrderKey());
        return Response.success(flag);
    }
}
